package com.wefans.wefansapp.activity;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wefans.wefansapp.R;
import com.wefans.wefansapp.common.tool.data.MFDataManager;
import com.wefans.wefansapp.common.tool.data.ResMyPointCouponData;
import com.wefans.wefansapp.common.tool.debug.MFDebug;
import com.wefans.wefansapp.common.tool.network.MFNetworkManager;
import com.wefans.wefansapp.common.tool.network.MFNetworkManager.IOnNetworkListener;
import com.wefans.wefansapp.common.tool.network.model.NetJsonData;
import com.wefans.wefansapp.common.tool.network.model.NetMessage;
import com.wefans.wefansapp.common.tool.preference.MFSharedPreference;
import com.wefans.wefansapp.common.tool.utils.MFToastUtil;

public class MyPointCouponActivity extends BaseActivity {

	private Context mContext = null;
	
	private DrawerLayout   m_DrawerLayout  = null;
    private FrameLayout    m_DrawerView    = null;
		
    private int nCurrentTabIdx = -1;
    private String mStrMaxIdx = "";
    private TextView mTvCouponCount = null;
	private Button[] mBtnTabArray =  new Button[3];
	private ListView mLvCoupon = null;
	private MyPointAdapter mAdtCoupon = null;
	private Button mBtnMore = null;
	
	private ArrayList<Object> mCouponList = new ArrayList<Object>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		mContext = this;
		
		setContentView(R.layout.activity_main);
		
		initialize();
        installEvent();
	}
	
	@Override
    protected void onPause()
    {
        MFDebug.d(">> onPause()");
        
        notifyDoClaseDrawerMenu();
        
        super.onPause();
    }

    @Override
    protected void notifyDoClaseDrawerMenu()
    {
        MFDebug.d(">> notifyDoClaseDrawerMenu()");
        
        super.notifyDoClaseDrawerMenu();
        
        if( m_DrawerLayout == null ) {
            MFDebug.e(new Exception("There are NO Drawer-Layout"));
            return;
        }
        
        if( m_DrawerView == null ) {
            MFDebug.e(new Exception("There are NO Drawer-View"));
            return;
        }
        
        m_DrawerLayout.closeDrawer(m_DrawerView);
        m_sdQuickMenu.close();
    }
	
	private void initialize()
    {
        MFDebug.d(">> initialize()");
        
        //Contents layout adding
        FrameLayout containerContents = (FrameLayout) findViewById(R.id.FL_MAIN_CONTAINER);
        addContentsLayout(containerContents, R.layout.activity_mypoint_coupon);
        addQuickMenuLayout(containerContents);
        
        //Drawer menu adding
        m_DrawerView = (FrameLayout) findViewById(R.id.FL_DRAWER_LAYOUT);
        addDrawerLayout(m_DrawerView);
        
        //set drawer layout (root)
        m_DrawerLayout = (DrawerLayout) findViewById(R.id.DL_MAIN_NAVIGATION_LAYOUT);
    }
    
    private void installEvent()	
    {
        MFDebug.d(">> installEvent()");
        
        ((ImageButton) findViewById(R.id.IB_MAIN_HEADER_LEFT_BUTTON)).setOnClickListener(this);
        ((ImageButton) findViewById(R.id.IB_MAIN_HEADER_RIGHT_BUTTON)).setOnClickListener(this);
        
        mTvCouponCount = (TextView)findViewById(R.id.TV_MYCOUPON_COUNT);
        mBtnTabArray[0] = (Button)findViewById(R.id.BTN_COUPONPOINT_ALL);
        mBtnTabArray[1] = (Button)findViewById(R.id.BTN_COUPONPOINT_SAVE);
        mBtnTabArray[2] = (Button)findViewById(R.id.BTN_COUPONPOINT_USE);
        mLvCoupon = (ListView)findViewById(R.id.LV_COUPON);
        mBtnMore = (Button)findViewById(R.id.BTN_MORE);
        
        mBtnTabArray[0].setOnClickListener(this);
        mBtnTabArray[1].setOnClickListener(this);
        mBtnTabArray[2].setOnClickListener(this);
        mBtnMore.setOnClickListener(this);
        
        mAdtCoupon = new MyPointAdapter(mContext, mCouponList);
        mLvCoupon.setAdapter(mAdtCoupon);
        
        updateList(0);
    }
    
	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.BTN_MORE:
			updateMore();
			break;
		case R.id.BTN_COUPONPOINT_ALL:
			updateList(0);
			break;
		case R.id.BTN_COUPONPOINT_SAVE:
			updateList(1);
			break;
		case R.id.BTN_COUPONPOINT_USE:
			updateList(2);
			break;
		}
		super.onClick(v);
	}
	
	private void updateList(int nTabIdx) {
		if (nCurrentTabIdx == nTabIdx) {
			return;
		} else {
			mCouponList.clear();
			nCurrentTabIdx = nTabIdx;
			mStrMaxIdx = "";
			
			mBtnTabArray[0].setSelected(false);
	        mBtnTabArray[1].setSelected(false);
	        mBtnTabArray[2].setSelected(false);
	        
	        mBtnTabArray[nCurrentTabIdx].setSelected(true);
			
			requestPointCouponData(nTabIdx, mStrMaxIdx);
		}
	}
	
	private void updateMore() {
		requestPointCouponData(nCurrentTabIdx, mStrMaxIdx);
	}
	
	private void requestPointCouponData(int nTabType, String strMaxIdx) {
		
		String strUrl = MFNetworkManager.BASE_URL + "/my/get_ticket.php";
		strUrl = strUrl + "?mem_idx=" + MFDataManager.getInstance(getApplicationContext()).getMebmerIdx();
		strUrl = strUrl + "&type=" +  nTabType;
		strUrl = strUrl + "&max_idx=" + strMaxIdx;
		
		MFNetworkManager.getInstance(this).requestGetData(MFNetworkManager.REQUEST_POINT_COUPON_lIST, strUrl, mNetworkListener);
	}
	
	IOnNetworkListener mNetworkListener = new IOnNetworkListener() {
		
		@Override
		public void onNetMessage(int resultCode, NetMessage message) {
			try {
	            //서버 여동 데이터
	            if( message instanceof NetJsonData ) {
	                long magicNum = ((NetJsonData) message).getMagicNum();
	                String json = ((NetJsonData) message).getResponseJson();
	                
	                if( json == null ) {
	                    throw new Exception("invalid json data");
	                }
	                
	                Gson gson = new Gson();
	                
	                if (magicNum == MFNetworkManager.REQUEST_POINT_COUPON_lIST) {
	                	
	                	ResMyPointCouponData resultData = (ResMyPointCouponData)gson.fromJson(json, ResMyPointCouponData.class);
	                    if( resultData == null ) {
	                        throw new Exception("Gson parsing error");
	                    }
	                	
	                	if (resultData.isSuccsess()) {

	                		mStrMaxIdx = resultData.getMax_idx();
	                		mTvCouponCount.setText(resultData.getMy_ticket());
	                		
	                		if (resultData.getData() != null) {
	                			mCouponList.addAll(resultData.getData());
		                		mAdtCoupon.notifyDataSetChanged();
	                		}
	                	} else {
	                		MFToastUtil.NetResponseToast(getApplicationContext(), resultData.getResultcode(), resultData.getResultmsg());
	                	}
	                }
	            }	            
	        } catch(Exception e) {
	            MFDebug.e(e);
	        }		
		}
	};
}
